package com.sevenre.trackre.driver;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.sevenre.trackre.driver.datatypes.Trip;
import com.sevenre.trackre.driver.network.Server;
import com.sevenre.trackre.driver.utils.Constants;
import com.sevenre.trackre.driver.utils.NetworkConnectivity;
import com.sevenre.trackre.driver.utils.Utils;

public class ActivityLogin extends Activity implements OnClickListener {

	private static final int MY_DATA_CHECK_CODE = 110;
	EditText vehicleNo;
	Button enter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_login);
		setUpUI();
	}

	private void setUpUI() {
		vehicleNo = (EditText) findViewById(R.id.login_vehicle_no);
		enter = (Button) findViewById(R.id.login_enter);
		vehicleNo.setGravity(Gravity.CENTER);
		enter.setOnClickListener(ActivityLogin.this);
		vehicleNo.setTypeface(Utils.getTypeFace(getAssets(),Utils.roboto));
		enter.setTypeface(Utils.getTypeFace(getAssets(),Utils.roboto));
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.login_enter:
			String vehicle = vehicleNo.getText().toString();
			if (vehicle.length() < Constants.VEHICLE_NO_LENGTH){
				Toast.makeText(getApplicationContext(), "Vehicle No is not correct", Toast.LENGTH_LONG).show();
			} else {
				new GetVehicleId().execute(vehicle);
			}
		}
	}
	
	public class GetVehicleId extends AsyncTask<String, Integer, String> {

		@Override
		protected String doInBackground(String... params) {
			return Server.getVehicleId(params[0]);
		}
		
		@Override
		protected void onPostExecute(String result) {
			Constants.VEHICLE_ID = result;
			Intent i = new Intent(ActivityLogin.this, ActivityHome.class);
			startActivity(i);
			super.onPostExecute(result);
		}
		
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		if (!NetworkConnectivity.isConnectedToInternet(getApplicationContext())){
			AlertDialog.Builder builder = new AlertDialog.Builder(ActivityLogin.this);
			builder.setTitle("Network connectivity not found");
			builder.setMessage("Please start your Data Plan");
			builder.setPositiveButton("Yes", new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					startActivity(Utils.intentForNetworkAccess());
				}
			});
			builder.setNegativeButton("No", new android.content.DialogInterface.OnClickListener() {
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.dismiss();
				}
			});
			builder.create().show();
		}
	}
}
